<template>
  <div class="phone-login-container">
    <h2>手机号验证码登录</h2>
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" label-width="120px">
      <el-form-item label="手机号" prop="phoneNumber">
        <el-input v-model="loginForm.phoneNumber" type="number" />
      </el-form-item>
      <el-form-item label="验证码" prop="verificationCode">
        <el-input v-model="loginForm.verificationCode" type="number" />
        <el-button type="text" @click="sendVerificationCode" :disabled="isSendingCode">
          {{ isSendingCode? '重新发送(' + remainingSeconds + 's)' : '发送验证码' }}
        </el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="login" :disabled="!isFormValid">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  name: 'PhoneLogin',
  data() {
    return {
      loginForm: {
        phoneNumber: '',
        verificationCode: ''
      },
      loginRules: {
        phoneNumber: [
          { required: true, message: '请输入手机号', trigger: 'blur' }
        ],
        verificationCode: [
          { required: true, message: '请输入验证码', trigger: 'blur' }
        ]
      },
      isSendingCode: false,
      remainingSeconds: 60,
      timer: null,
      isFormValid: false
    };
  },
  methods: {
    // 发送验证码
    sendVerificationCode() {
      const phoneNumber = this.loginForm.phoneNumber;
      if (!phoneNumber) {
        this.$message.error('请先输入手机号');
        return;
      }

      this.isSendingCode = true;

      // 假设后端发送验证码的接口是 /api/sendVerificationCode，需要根据实际情况修改
      axios.post('/api/sendVerificationCode', { phoneNumber })
        .then((res) => {
          if (res.data.code === 200) {
            this.$message.success('验证码已发送，请查收');
            this.startTimer();
          } else {
            this.$message.error(res.data.msg);
            this.isSendingCode = false;
          }
        })
        .catch((error) => {
          this.$message.error('发送验证码失败，请稍后再试');
          this.isSendingCode = false;
        });
    },

    // 启动倒计时定时器
    startTimer() {
      this.timer = setInterval(() => {
        if (this.remainingSeconds <= 0) {
          clearInterval(this.timer);
          this.isSendingCode = false;
          this.remainingSeconds = 60;
        } else {
          this.remainingSeconds--;
        }
      }, 1000);
    },

    // 登录验证
    login() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.isFormValid = true;

          const { phoneNumber, verificationCode } = this.loginForm;

          // 假设后端验证手机号和验证码并登录的接口是 /api/phoneLogin，需要根据实际情况修改
          axios.post('/api/phoneLogin', { phoneNumber, verificationCode })
            .then((res) => {
              if (res.data.code === 200) {
                this.$message.success('登录成功');
                // 这里可以根据业务需求进行后续操作，比如跳转到首页等
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch((error) => {
              this.$message.error('登录失败，请稍后再试');
            });
        } else {
          this.isFormValid = false;
        }
      });
    }
  },
  beforeDestroy() {
    // 在组件销毁时清除定时器，避免内存泄漏
    if (this.timer) {
      clearInterval(this.timer);
    }
  }
};
</script>

<style>
.phone-login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-color: #f4f4f4;
}

.el-form {
  background-color: white;
  padding: 20px;
  border-radius: 5px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
</style>
